<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="./biaodashi.js"></script>
    <title>黄金分割法</title>
</head>

<body>
    <div class="box">
        <h1 style="text-align: center;">黄金分割法</h1>
        <div class="row">
            函数:<input id="func" style="flex: 1;" type="text">
        </div>
        <div>(未知数用x代替，只支持一元多次,+-*/()运算符,次幂写成x*x形式)</div>
        <div class="row">
            区间：<input id="areaA" style="flex: 1;" type="number">
            -- <input id="areaB" style="flex: 1;" type="number">
            精确度：<input id="limit" style="flex: 1;" type="number">
        </div>
        <div>
            <button style="float: right; padding: 0 10px;" onclick="clearPrint()">清 屏</button>
            <button style="float: right; padding: 0 10px;margin-right: 20px;" onclick="allClear()">重 输</button>
            <button style="float: right; padding: 0 10px;margin-right: 20px;" onclick="submit()">计 算</button>
        </div>
        输出内容：
        <textarea id="print" style="flex: 1;resize:none;background: white;" readonly></textarea>
    </div>

</body>
<script>

    function Compute() {
        // let a, b, e, x1, x2, y1, y2;
        this.a = parseFloat(document.getElementById("areaA").value);
        this.b = parseFloat(document.getElementById("areaB").value);
        this.e = parseFloat(document.getElementById("limit").value);

        if (!(this.a && this.b && this.e)) {
            writeLine("输入数值类型不正确!")
            return
        }
        this.funcs = document.getElementById("func").value
        this.countL = function () {
            return this.a + 0.382 * (this.b - this.a)
        }
        this.countR = function () {
            return this.a + 0.618 * (this.b - this.a)
        }
        this.getF = function (x) {
            return parseFloat(computeStr(this.funcs.replace(/x/g, x)).toFixed(10))
        }
    }
    function allClear() {
        document.getElementById("func").value = ""
        document.getElementById("areaA").value = ""
        document.getElementById("areaB").value = ""
        document.getElementById("limit").value = ""
        document.getElementById("print").value = ""
    }
    function clearPrint(){
        document.getElementById("print").value = ""
    }
    function submit() {
        let com = new Compute()
        com.x1 = com.countL()
        com.x2 = com.countR()
        com.y1 = com.getF(com.x1)
        com.y2 = com.getF(com.x2)
        writeLine(`已知a=${com.a},b=${com.b},ε=${com.e};`)
        writeLine(`x1=a+0.382(b-a)=${com.x1},`)
        writeLine(`x2=a+0.618(b-a)=${com.x2},`)
        writeLine(`f(x1)=${com.y1},f(x2)=${com.y2};`)
        while (true) {
            if (com.y1 > com.y2) {
                com.a = com.x1
                writeLine(`∵f(x1)>f(x2),∴a=x1=${com.a};`)
                if (com.b - com.a < com.e) {
                    printfAns()
                    return
                } else {
                    writeLine(`b-a=${com.b - com.a}≥${com.e};`)
                    com.y1 = com.y2
                    com.x1 = com.x2
                    com.x2 = com.countR()
                    com.y2 = com.getF(com.x2)
                    writeLine(`f(x1)=f(x2)=${com.y1},x1=x2=${com.x1},`)
                    writeLine(`x2=a+0.618(b-a)=${com.x2},f(x2)=${com.y2};`)
                }
            } else {
                com.b = com.x2
                writeLine(`∵f(x1)≤f(x2),∴b=x2=${com.b};`)
                if (com.b - com.a < com.e) {
                    printfAns()
                    return
                } else {
                    writeLine(`b-a=${com.b - com.a}≥${com.e};`)
                    com.y2 = com.y1
                    com.x2 = com.x1
                    com.x1 = com.countL()
                    com.y1 = com.getF(com.x1)
                    writeLine(`f(x2)=f(x1)=${com.y2},x2=x1=${com.x2},`)
                    writeLine(`x1=a+0.382(b-a)=${com.x1},f(x1)=${com.y1};`)
                }
            }
        }
        function printfAns(){
            writeLine("∵b-a<ε,求得最小值x*=(b+a)/2=" + (com.a + com.b) / 2)
        }
    }
    function writeLine(str) {
        document.getElementById("print").value += (str + '\n')
    }
    function write(str) {
        document.getElementById("print").value += str
    }
</script>
<style>
    html,
    body {
        height: 100%;
        margin: 0;
    }

    .box {
        display: flex;
        flex-direction: column;
        width: 600px;
        min-height: 600px;
    }

    .row {
        display: flex;
        margin: 10px 0;
    }

    body {
        display: flex;
        justify-content: center;
        align-items: center;
        vertical-align: middle;
        background: gold;
    }

    input {
        min-width: 0;
    }
</style>

</html>